package service

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"shrimp-blog-server/app/dao"
	"shrimp-blog-server/app/model"
)

func (server LinkService) AddLink(req *ghttp.Request, links ...model.Link) {
	if len(links) == 0 {
		responses.ErrorResponseExit(req, "请输入链接信息")
	}

	result, err := dao.Link.Insert(links...)
	if err != nil {
		g.Log("添加链接失败").Error(err)
		panic(gerror.Wrap(err, "添加链接失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "添加链接失败")
	}
	responses.SuccessResponse(req)
}

func (server LinkService) DeleteLink(req *ghttp.Request, id ...uint) {
	if len(id) == 0 {
		responses.ErrorResponseExit(req, "请输入链接id")
	}
	models := make([]model.Link, len(id))
	for i, v := range id {
		models[i] = model.Link{Id: v, Status: model.NotShowStatus}
	}

	result, err := dao.Link.Delete(models...)
	if err != nil {
		g.Log("删除链接失败").Error(err)
		panic(gerror.Wrap(err, "删除链接失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "删除链接失败")
	}
	responses.SuccessResponse(req)
}

func (server LinkService) UpdateLink(req *ghttp.Request, links ...model.Link) {
	if len(links) == 0 {
		responses.ErrorResponseExit(req, "请输入链接信息")
	}
	result, err := dao.Link.Update(links...)
	if err != nil {
		g.Log("更新链接失败").Error(err)
		panic(gerror.Wrap(err, "更新链接失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "更新链接失败")
	}
	responses.SuccessResponse(req)
}
